﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LevelEditor.App;
using LevelEditor.Objects.Tiles;
using LevelEditor.Objects;
using LevelEditor.Components;
using LevelEditor.Components.Layers;
using LevelEditor.Objects.Layers;
using LevelEditor.Components.Waypoints;
using System.Runtime.InteropServices;
using DialogsEditor;

namespace LevelEditor
{
    public partial class  LevelEditor : Form
    {

        public static bool IsCreatingConnectors{get; set;}

        private Bitmap waypointsCursor;

        public LevelEditor()
        {
            InitializeComponent();

            layerPanel1.SetForm(this);
            libraryPanel.SetForm(this);
            this.propertiesPanel1.setForm(this);

            this.libraryLabel.Parent = libaryLabelBackground;
            this.libraryLabel.BackColor = Color.Transparent;
            this.label5.Parent = pictureBox6;
            this.label1.Parent = this.pictureBox2;
            this.label2.Parent = this.pictureBox3;
            this.label3.Parent = this.pictureBox4;
            this.label4.Parent = this.pictureBox5;

            loadAsset.Filter = "PNG Images: |*.png"; // only PNG or JPG

            this.AllowDrop = true;


            this.stage1.SetEditor(this);
            // initializes all the tile types. ALWAYS call this method
            Tile.InitializeTiletypes();
            this.libraryPanel.UpdateSessionsIds();

            waypointsCursor = new Bitmap(@"Images\Pen-icon.png");

            IsCreatingConnectors = false;
        }

        private void importBtn_Click(object sender, EventArgs e)
        {
            if (loadAsset.ShowDialog() == DialogResult.OK)
            {
                LibraryItem i = Importer.Import(loadAsset.FileName, this);
                if (e == null)
                    return;

                libraryPanel.Controls.Add(i);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewLayer newLayer = new NewLayer(this);
            DialogResult result = newLayer.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                layerPanel1.NewLayer(newLayer.getLayerName(), newLayer.getCols(), newLayer.getRows(), newLayer.getTileWidth(), newLayer.getTileHeight());
            }

        }

        private void removeLayer_Click(object sender, EventArgs e)
        {
            layerPanel1.DeleteCurrentLayer();
        }

        private void copyLayer_Click(object sender, EventArgs e)
        {
            layerPanel1.CopyCurrentLayer();
        }

        private void changeLayerVisibility_Click(object sender, EventArgs e)
        {
            LayerPanel.CurrentLayer.ChangeVisibility();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            libraryPanel.CopyItem();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            libraryPanel.DeleteItem();
        }

        private void updateLayerPreview_Tick(object sender, EventArgs e)
        {
            if (Tile.CurrentSelectedTile == null || !Tile.CurrentSelectedTile.HasMoved) return;

            Tile.CurrentSelectedTile.MyLayer.GeneratePreview();
        }

        private void waypointsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!stage1.IsEdittingWaypoints())
            {

                if (!stage1.EditWaypoints()) // fail
                    return;

                Cursor myCursor = CreateCursor(waypointsCursor, 3, 3);
                Cursor = myCursor;
            }
            else
            {
                Cursor = Cursors.Arrow;
                stage1.StopEdittingWaypoints();
            }
        }

        public void EnableWaypointsEditor()
        {
            waypointsToolStripMenuItem.Enabled = true;
        }

        public void DisableWaypointsEditor()
        {
            waypointsToolStripMenuItem.Enabled = false;
        }

        public struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }

        [DllImport("user32.dll")]
        public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

        public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            IntPtr ptr = bmp.GetHicon();
            IconInfo tmp = new IconInfo();
            GetIconInfo(ptr, ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            ptr = CreateIconIndirect(ref tmp);
            return new Cursor(ptr);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveLevel.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            
            SaveHelper saveHelper = new SaveHelper(layerPanel1, libraryPanel);
            saveHelper.Save(saveLevel.FileName);
            
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (loadLevelDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            LoadHelper loadHelper = new App.LoadHelper(this, layerPanel1, libraryPanel);
            loadHelper.LoadLevel(loadLevelDialog.FileName);
        }

        private void behaviorsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            NewBehavior behaviorForm = new NewBehavior();
            behaviorForm.Show();
        }


        private void triggersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewTrigger triggerForm = new NewTrigger();
            triggerForm.Show();
        }

        private void dialogsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 dialogForm = new Form1();
            dialogForm.Show();
        }

        private void createToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //desabilitar o drag de todos os tiles
            if (!IsCreatingConnectors)
            {
                createToolStripMenuItem.Text = "Leave Create";
                IsCreatingConnectors = true;
            }
            else
            {
                createToolStripMenuItem.Text = "Create Connectors";
                IsCreatingConnectors = false;
            }
        }

        private void behaviorTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
